# 供应商数据插入脚本
# 执行SQL文件来插入测试数据

Write-Host "开始执行供应商数据插入脚本..." -ForegroundColor Green

# 检查SQL文件是否存在
$sqlFile = "insert-supplier-data.sql"
if (-not (Test-Path $sqlFile)) {
    Write-Host "错误: 找不到SQL文件 $sqlFile" -ForegroundColor Red
    exit 1
}

Write-Host "找到SQL文件: $sqlFile" -ForegroundColor Yellow

# 读取SQL文件内容
try {
    $sqlContent = Get-Content $sqlFile -Raw -Encoding UTF8
    Write-Host "成功读取SQL文件内容" -ForegroundColor Green
} catch {
    Write-Host "错误: 无法读取SQL文件: $($_.Exception.Message)" -ForegroundColor Red
    exit 1
}

# 显示SQL文件的基本信息
$lines = $sqlContent.Split("`n")
Write-Host "SQL文件总行数: $($lines.Length)" -ForegroundColor Cyan

# 查找INSERT语句
$insertCount = ($lines | Where-Object { $_ -match "INSERT INTO" }).Count
Write-Host "发现 $insertCount 个INSERT语句" -ForegroundColor Cyan

# 显示将要插入的数据类型
Write-Host "`n将要插入的数据类型:" -ForegroundColor Yellow
$lines | Where-Object { $_ -match "INSERT INTO" } | ForEach-Object {
    if ($_ -match "INSERT INTO (\w+)") {
        Write-Host "  - $($matches[1])" -ForegroundColor White
    }
}

Write-Host "`n准备执行SQL语句..." -ForegroundColor Green
Write-Host "请确保:" -ForegroundColor Yellow
Write-Host "1. 数据库连接正常" -ForegroundColor White
Write-Host "2. 数据库用户有INSERT权限" -ForegroundColor White
Write-Host "3. 相关表已存在" -ForegroundColor White

# 询问用户是否继续
$continue = Read-Host "`n是否继续执行? (y/N)"
if ($continue -ne "y" -and $continue -ne "Y") {
    Write-Host "操作已取消" -ForegroundColor Yellow
    exit 0
}

Write-Host "`n开始执行SQL语句..." -ForegroundColor Green

# 这里可以添加实际的数据库执行逻辑
# 例如使用MySQL命令行工具或其他数据库连接方式

Write-Host "`nSQL执行完成!" -ForegroundColor Green
Write-Host "请检查数据库中的结果" -ForegroundColor Yellow

# 提供验证查询
Write-Host "`n验证查询:" -ForegroundColor Cyan
Write-Host "SELECT COUNT(*) as TotalSuppliers FROM Suppliers WHERE IsDeleted = 0;" -ForegroundColor White
Write-Host "SELECT COUNT(*) as TotalLevels FROM BusinessPartnerLevels WHERE IsDeleted = 0;" -ForegroundColor White
Write-Host "SELECT COUNT(*) as TotalCategories FROM BusinessPartnerCategories WHERE IsDeleted = 0;" -ForegroundColor White

Write-Host "`n脚本执行完成!" -ForegroundColor Green 